package appapi.service.impl;

import appapi.Request.Basic.StaffReq;
import appapi.Request.StaffPageReq;
import appapi.Global.BusinessException;
import appapi.dao.OperatorMapper;
import appapi.dao.StaffMapper;
import appapi.dto.EditPwdDto;
import appapi.entity.Zhongjiao.OperatorDto;
import appapi.entity.Zhongjiao.StaffDto;
import appapi.service.IStaffService;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * @author djq
 * @date 2023/5/10 9:40
 * @description:
 */
@Component
public class StaffService implements IStaffService {
    @Resource
    private StaffMapper staffMapper;
    @Autowired
    BCryptPasswordEncoder bCryptPasswordEncoder;
    @Resource
    OperatorMapper operatorMapper;

    /**
     * 新增用户
     *
     * @param dto
     * @return
     */
    @Override
    public int InsertStaff(StaffDto dto) {

        Map<String, Object> staffDto = staffMapper.selectAccount(dto.getStaffAccount());
        //判断账号是否存在,存在则返回
        if (staffDto != null) {
            throw new BusinessException(500, "账号已存在!");
        }

        //查询该人员是否在操作人员也有账号
        List<OperatorDto> operatorDtos = operatorMapper.selectAllByOperMobile(dto.getStaffMobile());
        if (operatorDtos.size()>0) {
            //有则加入微信id
            dto.setStaffWxid(operatorDtos.get(0).getWxId());
        }
        dto.setStaffPwd(dto.getStaffMobile().substring(dto.getStaffMobile().length()-6));
        //密码加密保存
        dto.setStaffPwd(bCryptPasswordEncoder.encode(dto.getStaffPwd()));
        dto.setCreateTime(LocalDateTime.now());
        dto.setUpdateTime(LocalDateTime.now());
        int insert = staffMapper.insertSelective(dto);

        return insert;
    }

    /**
     * 修改用户
     *
     * @param dto
     * @return
     */
    @Override
    public int UpdateStaff(StaffDto dto) {

        StaffDto staffDto = staffMapper.selectByPrimaryKey(dto.getStaffId());
        //判断账号是否存在,不存在则返回
        if (staffDto == null) {
            throw new BusinessException(500, "账号不存在!");
        }

        if (staffDto.getStaffId()==1){
            throw new BusinessException(-1,"超级管理账号不允许编辑!");
        }

        dto.setUpdateTime(LocalDateTime.now());

        int result = staffMapper.updateByPrimaryKeySelective(dto);
        return result;
    }

    /**
     * 删除用户
     *
     * @param staffId
     * @return
     */
    @Override
    public int DelStaff(List<Long> staffId) {
        //StaffDto staffDto = staffMapper.selectByPrimaryKey(staffId);
        //判断账号是否存在,不存在则返回
//        if (staffDto == null) {
//            throw new BusinessException(500, "账号不存在!");
//        }
        staffId.forEach(id->{
            if (id==1){
                throw new BusinessException(-1,"管理员账号不允许删除!");
            }
        });

        int result = staffMapper.deleteByIds(staffId);
        if (result == 0){
            throw new BusinessException(500,"超级管理删除账号失败!");
        }

//        if (result!=staffId.size()){
//            throw new BusinessException(500,"存在已被删除账号!");
//        }

        return result;
    }

    /**
     * 重置密码
     *
     * @param staffId
     * @return
     */
    @Override
    public int ResetPwd(Long staffId) {
        StaffDto staffDto = staffMapper.selectByPrimaryKey(staffId);
        //判断账号是否存在,不存在则返回
        if (staffDto == null) {
            throw new BusinessException(500, "账号不存在!");
        }
        staffDto.setStaffPwd(bCryptPasswordEncoder.encode(staffDto.getStaffMobile().substring(5,11)));


        staffDto.setUpdateTime(LocalDateTime.now());
        return staffMapper.updateByPrimaryKeySelective(staffDto);
    }

    /**
     * 查询用户列表
     *
     * @param dto
     * @return
     */
    @Override
    public List<Map<String, Object>> SelectStaffPage(StaffPageReq dto) {
        PageHelper.startPage(dto.getCurrPage(), dto.getPageSize());
        List<Map<String, Object>> staffDtos = staffMapper.selectStaffs(dto);
        return staffDtos;
    }

    /**
     * 查询用户列表
     *
     * @param dto
     * @return
     */
    @Override
    public List<StaffDto> SelectStaffList(StaffReq dto) {
        return staffMapper.selectStaffList(dto);
    }


    /**
     * 查询用户详情
     *
     * @param staffId
     * @return
     */
    @Override
    public Map<String, Object> SelectStaffDetail(Long staffId) {

        Map<String, Object> staffDetail = staffMapper.selectStaffDetail(staffId);

        return staffDetail;
    }

    @Override
    public void UpdatePwd(EditPwdDto dto) {
        StaffDto staffDto = staffMapper.selectByPrimaryKey(dto.getStaffId());
        //判断账号是否存在,不存在则返回
        if (staffDto == null) {
            throw new BusinessException(500, "账号不存在!");
        }
        if (ObjectUtils.isEmpty(dto.getPassWord())){
            throw new BusinessException(500,"密码不能为空");
        }
        staffDto.setStaffPwd(bCryptPasswordEncoder.encode(dto.getPassWord()));

        staffMapper.updateByPrimaryKeySelective(staffDto);
    }
}
